﻿using BLL;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Model;

namespace TiboShop.Controllers
{
    public class CommodityController : Controller
    {

        private readonly BLLCommodity bllCommodity;
        private readonly ISession _session;
        private readonly IHttpContextAccessor _contextAccessor;
        private readonly BLLType _type;
        private readonly IWebHostEnvironment _webHostEnvironment;
        public CommodityController(BLLCommodity commodity,  IHttpContextAccessor contextAccessor,BLLType type,IWebHostEnvironment webHost)
        {
            bllCommodity = commodity;
            _contextAccessor = contextAccessor;
            _session = _contextAccessor.HttpContext.Session;
            _type = type;
            _webHostEnvironment = webHost;
        }

        [HttpPost]
        public IActionResult UploadCommodity(string name, double price, string discribe, IFormFile img, string position, int totalnumber, int tid)
        {
            

            if (img == null || img.Length == 0)
                return Content("文件不能为空");

            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", string.Format("{0}_{1}", DateTime.Now.Ticks, img.FileName));

            using (var stream = new FileStream(path, FileMode.Create))
            {
                img.CopyTo(stream);
            }

            int len = "D:\\Web代码\\TiboShop\\wwwroot".Length;
            string url = path.Substring(len);
            url = ".."+url;
            if (bllCommodity.AddCommodity(name, price, discribe, url, position, totalnumber,Convert.ToInt32(_session.GetInt32("UID")), tid))
            {
                
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "添加成功！";
                messageBoxVO.RedrectUrl = "Manage";
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
            else
            {
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "添加失败！请重新添加";
                messageBoxVO.RedrectUrl = "UploadCommodity";
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
        }
        [HttpGet]
        public IActionResult UploadCommodity()
        {
            List<TypeClass> tyclass =_type.GetAllType();
            List<SelectListItem> _lstr = new List<SelectListItem>();
            foreach (TypeClass r in tyclass)
            {
                _lstr.Add(new SelectListItem { Text = r.Name, Value = r.ID.ToString() });    
            }
            ViewBag.ComType= _lstr;
            return View();
        }
        [HttpGet]
        public IActionResult Manage()
        {
            int uid = Convert.ToInt32(_session.GetInt32("UID"));
            return View(bllCommodity.UserCommodity(uid));
        }
        [HttpGet]
        public IActionResult Index(int cid)
        {
            ViewBag.role = Convert.ToInt32(_session.GetInt32("URID"));
            return View(bllCommodity.GetCommodityByID(cid));
        }
        [HttpGet]
        public IActionResult EditCommodity(int cid)
        {
            List<TypeClass> tyclass = _type.GetAllType();
            List<SelectListItem> _lstr = new List<SelectListItem>();
            Commodity commodity = new Commodity();
            commodity = bllCommodity.GetCommodityByID(cid);
            foreach (TypeClass r in tyclass)
            {
                bool exist=false;
                if(r.ID==commodity.type.ID)
                    exist=true;
                _lstr.Add(new SelectListItem { Text = r.Name, Value = r.ID.ToString(),Selected= exist});
            }
            ViewBag.ComType = _lstr;
            return View(bllCommodity.GetCommodityByID(cid));
        }
        [HttpPost]
        public IActionResult EditCommodity(int cid,string name, double price, string discribe, IFormFile img, string position, int totalnumber, int bid, int tid)
        {
            if (img == null || img.Length == 0)
                return Content("文件不能为空");

            var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images", string.Format("{0}_{1}", DateTime.Now.Ticks, img.FileName));

            using (var stream = new FileStream(path, FileMode.Create))
            {
                img.CopyTo(stream);
            }

            int len = "D:\\Web代码\\TiboShop\\wwwroot".Length;
            string url = path.Substring(len);
            url = ".." + url;
            if (bllCommodity.ChangeCommodity(cid, name, price, discribe, url, position, totalnumber, Convert.ToInt32(_session.GetInt32("UID")),tid))
            {
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "更改成功！";
                messageBoxVO.RedrectUrl = "Manage";
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
            else
            {
                MessageBoxVO messageBoxVO = new MessageBoxVO();
                messageBoxVO.Msg = "更改失败！请重试";
                messageBoxVO.RedrectUrl = "Manage";
                return View("Views\\Shared\\Components\\MessageBox\\Default.cshtml", messageBoxVO);
            }
        }
        [HttpGet]
        public void CommentInsert(string comment,int oid)
        {
            bllCommodity.insertComment(comment,oid,Convert.ToInt32(_session.GetInt32("UID")));
        }
    }
}
